/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package navigation {
	import org.splink.library.tree.Node;

	/**
	 * The <code>StatefulNode</code> adds state awareness to a <code>Node</code> object.
	 * State awareness means, the <code>StatefulNode</code> knows whether it is open or not
	 * and whether it has been closed recently or not.
	 * 
	 * @see Node
	 * @see Tree
	 * @author Max Kugland
	 */
	public class StatefulNode extends Node {
		private var _open : Boolean = false;
		private var _closed : Boolean;
		
		public function StatefulNode(id : String, data : * = null) {
			super(id, data);
		}

		/**
		 * Detects whether this <code>StatefulNode</code> has open children
		 * 
		 * @return true if the <code>StatefulNode</code> has open children otherwise false
		 */
		public function hasOpenChilds() : Boolean {
			for each (var node : StatefulNode in children) {
				if(node.open) {
					return true;
				}
			}
			return false;
		}

		/**
		 * Sets whether the <code>StatefulNode</code> has been closed recently or not
		 * 
		 * @param closed a Boolean value
		 */
		public function set closed(closed : Boolean) : void {
			_closed = closed;
		}

		/**
		 * @return the <code>StatefulNode</code>'s closed status
		 */
		public function get closed() : Boolean {
			return _closed;
		}

		/**
		 * Sets whether this <code>StatefulNode</code> is open or not.
		 * 
		 * @param open a Boolean value
		 */
		public function set open(open : Boolean) : void {
			_open = open;
		}

		/**
		 * @return this <code>StatefulNode</code>'s open status
		 */
		public function get open() : Boolean {
			return _open;
		}
		
		/**
		 * @inheritDoc
		 */
		override public function toString() : String {
			var parentId : String = parent != null ? parent.id : null;
			return "org.splink.library.tree.StatefulNode id: " + id + " parentId: " + parentId + " layer: " + layer + " data: " + data+" _closed: " + _closed+" _open: " + _open;
		}
	}
}
